<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>AdWords::Generator</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
    <link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
    <script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
    <script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
</head>

<body>     
    <div class="banner">
        <h1>
            <span class="type">Module</span> 
            AdWords::Generator 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/lib/adwords4r/generator_rb.html">lib/adwords4r/generator.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
    
    <div class="description">
        <p>
Contains the methods that handle wrapper code generation.
</p>

    </div>
    

    

    
    

    
    
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
    
        <dt>G</dt>
        <dd>
            <ul>
                
                <li><a href="#M000072">generate_wrapper_class</a></li>
                
            </ul>
        </dd>
    
    </dl>
    

    

    

    

    
    <div class="sectiontitle">Constants</div>
    <table border='0' cellpadding='5'>
        
        <tr valign='top'>
            <td class="attr-name">ARRAY_CLASSNAME</td>
            <td>=</td>
            <td class="attr-value">'SOAP::SOAPArray'</td>
        </tr>
        
        
    </table>
    

    

    
            <div class="sectiontitle">Class Public methods</div>
            
            <div class="method">
                <div class="title" id="M000072">
                    
                    <a name="M000072"></a><b>generate_wrapper_class</b>(version, service)
                    
                </div>
                
                <div class="description">
                  <p>
Generate the wrapper class for a given service. These classes make it
easier to invoke the <a href="API.html">API</a> methods, by removing the
need to instance a <MethodName> object, instead allowing passing of the
call parameters directly. Here is an example of a generated wrapper class,
with one <a href="API.html">API</a> method and one extension:
</p>
<pre>
 class SampleServiceWrapper

   attr_reader :api

   def initialize(driver, api)
     @driver = driver
     @api = api
   end

   def getSomething(par1, par2)
     begin
       AdWords::Service.validate_param('par1', par1, SOAP::SOAPArray)
       AdWords::Service.validate_param('par2', par2, SOAP::SOAPInt)
       # Construct request object and make API call
       obj = AdWords::V13::GetSomething.new(par1, par2)
       return @driver.getSomething(obj)
     rescue SOAP::FaultError =&gt; fault
       raise(AdWords::Error::create_specific_api_error(fault),
           &quot;getSomething Call Failed: &quot; + fault.faultstring.to_s, caller)
     end
   end

   def doSomethingElseExtension(par1, par2)
     return AdWords::Extensions.doSomethingElseExtension(self, par1, par2)
   end
 end
</pre>
<p>
Args:
</p>
<ul>
<li>version: the <a href="API.html">API</a> version (as an integer)

</li>
<li>service: the service name (as a string)

</li>
</ul>
<p>
Returns: The Ruby code for the class, as a string.
</p>

                </div>
                
                
                
                
                <div class="sourcecode">
                    <p class="source-link">
                        Source: <a href="javascript:toggleSource('M000072_source')" id="l_M000072_source">show</a>
                        
                    </p>
                    <div id="M000072_source" class="dyn-source">
                        <pre><span class="ruby-comment cmt"># File lib/adwords4r/generator.rb, line 71</span>
    <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">generate_wrapper_class</span>(<span class="ruby-identifier">version</span>, <span class="ruby-identifier">service</span>)
      <span class="ruby-identifier">wrapper</span> = <span class="ruby-identifier">service</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;ServiceWrapper&quot;</span>
      <span class="ruby-identifier">module_name</span> = <span class="ruby-constant">AdWords</span><span class="ruby-operator">::</span><span class="ruby-constant">Service</span>.<span class="ruby-identifier">get_module_name</span>(<span class="ruby-identifier">version</span>, <span class="ruby-identifier">service</span>)
      <span class="ruby-identifier">driver</span> = <span class="ruby-constant">AdWords</span><span class="ruby-operator">::</span><span class="ruby-constant">Service</span>.<span class="ruby-identifier">get_interface_name</span>(<span class="ruby-identifier">version</span>, <span class="ruby-identifier">service</span>)
      <span class="ruby-identifier">driver_class</span> = <span class="ruby-identifier">eval</span>(<span class="ruby-identifier">driver</span>)

      <span class="ruby-identifier">registry</span> = <span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;#{module_name}::DefaultMappingRegistry::LiteralRegistry&quot;</span>)

      <span class="ruby-identifier">class_def</span> = <span class="ruby-value str">&quot;# This file was automatically generated during the \&quot;rake generate\&quot; step of\n# library setup.\nrequire 'adwords4r/v\#{version}/\#{service}ServiceDriver.rb'\n\nmodule AdWords\nmodule V\#{version}\nmodule \#{service}Service\n\n# Wrapper class for the v\#{version.to_s} \#{service} service.\n# This class is automatically generated.\nclass \#{wrapper}\n\n# Holds the AdWords::API object to which the wrapper belongs.\nattr_reader :api\n\n# Holds a shortcut to the parent module.\n# Use this to avoid typing the full class name when creating classes\n# belonging to this service, e.g.\n#  service_object.module::ClassName\n# instead of\n#  AdWords::V\#{version.to_s}::\#{service}Service::ClassName\n# This will make it easier to migrate your code between API versions.\nattr_reader :module\n\npublic\n\n# Constructor for \#{wrapper}.\n#\n# Args:\n# - driver: SOAP::RPC::Driver object with the remote SOAP methods for\n#   this service\n# - api: the AdWords::API object to which the wrapper belongs\n#\ndef initialize(driver, api)\n@driver = driver\n@api = api\n@module = AdWords::V\#{version.to_s}::\#{service}Service\nend\n\n&quot;</span>

      <span class="ruby-comment cmt"># Add service methods</span>
      <span class="ruby-identifier">methods</span> = <span class="ruby-identifier">driver_class</span><span class="ruby-operator">::</span><span class="ruby-constant">Methods</span>
      <span class="ruby-identifier">module_name</span> = <span class="ruby-constant">AdWords</span><span class="ruby-operator">::</span><span class="ruby-constant">Service</span>.<span class="ruby-identifier">get_module_name</span>(<span class="ruby-identifier">version</span>, <span class="ruby-identifier">service</span>)
      <span class="ruby-identifier">methods</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">method</span><span class="ruby-operator">|</span>
        <span class="ruby-identifier">name</span> = <span class="ruby-identifier">method</span>[<span class="ruby-value">1</span>]
        <span class="ruby-identifier">doc_link</span> = <span class="ruby-identifier">get_doc_link</span>(<span class="ruby-identifier">version</span>, <span class="ruby-identifier">service</span>, <span class="ruby-identifier">name</span>)
        <span class="ruby-identifier">method_def</span> = <span class="ruby-value str">&quot;# Calls the {\#{name}}[\#{doc_link}] method of the \#{service} service.\n# Check {the online documentation for this method}[\#{doc_link}].\n&quot;</span>

        <span class="ruby-identifier">method_class</span> = <span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;#{module_name}::#{fix_case_up(name)}&quot;</span>)
        <span class="ruby-identifier">arguments</span> = <span class="ruby-identifier">registry</span>.<span class="ruby-identifier">schema_definition_from_class</span>(<span class="ruby-identifier">method_class</span>).<span class="ruby-identifier">elements</span>

        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">arguments</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
          <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;#\n# Args:\n&quot;</span>
        <span class="ruby-keyword kw">end</span>

        <span class="ruby-comment cmt"># Add list of arguments to the RDoc comment</span>
        <span class="ruby-identifier">arguments</span>.<span class="ruby-identifier">each_with_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">elem</span>, <span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
          <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">get_type</span>(<span class="ruby-identifier">elem</span>) <span class="ruby-operator">==</span> <span class="ruby-constant">ARRAY_CLASSNAME</span>
            <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;# - \#{elem.varname}: \#{get_type(elem)} of \#{elem.mapped_class}\n&quot;</span>
          <span class="ruby-keyword kw">else</span>
            <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;# - \#{elem.varname}: \#{get_type(elem)}\n&quot;</span>
          <span class="ruby-keyword kw">end</span>
        <span class="ruby-keyword kw">end</span>

        <span class="ruby-identifier">response_class</span> = <span class="ruby-identifier">eval</span>(<span class="ruby-node">&quot;#{module_name}::#{fix_case_up(name)}Response&quot;</span>)
        <span class="ruby-identifier">returns</span> = <span class="ruby-identifier">registry</span>.<span class="ruby-identifier">schema_definition_from_class</span>(<span class="ruby-identifier">response_class</span>).<span class="ruby-identifier">elements</span>

        <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">returns</span>.<span class="ruby-identifier">size</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0</span>
          <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;#\n# Returns:\n&quot;</span>
        <span class="ruby-keyword kw">end</span>

        <span class="ruby-comment cmt"># Add list of returns to the RDoc comment</span>
        <span class="ruby-identifier">returns</span>.<span class="ruby-identifier">each_with_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">elem</span>, <span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
          <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">get_type</span>(<span class="ruby-identifier">elem</span>) <span class="ruby-operator">==</span> <span class="ruby-constant">ARRAY_CLASSNAME</span>
            <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;# - \#{elem.varname}: \#{get_type(elem)} of \#{elem.mapped_class}\n&quot;</span>
          <span class="ruby-keyword kw">else</span>
            <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;# - \#{elem.varname}: \#{get_type(elem)}\n&quot;</span>
          <span class="ruby-keyword kw">end</span>
        <span class="ruby-keyword kw">end</span>

        <span class="ruby-identifier">arg_names</span> = []
        <span class="ruby-identifier">arguments</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">elem</span><span class="ruby-operator">|</span>
          <span class="ruby-identifier">arg_names</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">elem</span>.<span class="ruby-identifier">varname</span>
        <span class="ruby-keyword kw">end</span>
        <span class="ruby-identifier">arg_list</span> = <span class="ruby-identifier">arg_names</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">', '</span>)

        <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;#\n# Raises:\n# Error::ApiError (or a subclass thereof) if a SOAP fault occurs.\n#\ndef \#{name}(\#{arg_list})\nbegin\n&quot;</span>

        <span class="ruby-comment cmt"># Add validation for every argument</span>
        <span class="ruby-identifier">arguments</span>.<span class="ruby-identifier">each_with_index</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">elem</span>, <span class="ruby-identifier">index</span><span class="ruby-operator">|</span>
          <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;AdWords::Service.validate_param('\#{elem.varname}',\n\#{arg_names[index]}, \#{get_type(elem)})\n&quot;</span>
        <span class="ruby-keyword kw">end</span>

        <span class="ruby-identifier">method_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;# Construct request object and make API call\nobj = \#{module_name}::\#{fix_case_up(name)}.new(\#{arg_list})\nreturn @driver.\#{name}(obj)\nrescue SOAP::FaultError =&gt; fault\nraise(AdWords::Error::create_specific_api_error(fault),\n\&quot;\#{name} Call Failed: \&quot; + fault.faultstring.to_s, caller)\nend\nend\n\n&quot;</span>
        <span class="ruby-identifier">class_def</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">method_def</span>
      <span class="ruby-keyword kw">end</span>

      <span class="ruby-comment cmt"># Add extension methods, if any</span>
      <span class="ruby-identifier">extensions</span> = <span class="ruby-constant">AdWords</span><span class="ruby-operator">::</span><span class="ruby-constant">Extensions</span><span class="ruby-operator">::</span><span class="ruby-identifier">extensions</span>[[<span class="ruby-identifier">version</span>, <span class="ruby-identifier">service</span>]]
      <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">nil?</span>
        <span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">ext</span><span class="ruby-operator">|</span>
          <span class="ruby-identifier">params</span> = <span class="ruby-constant">AdWords</span><span class="ruby-operator">::</span><span class="ruby-constant">Extensions</span><span class="ruby-operator">::</span><span class="ruby-identifier">methods</span>[<span class="ruby-identifier">ext</span>].<span class="ruby-identifier">join</span>(<span class="ruby-value str">', '</span>)
          <span class="ruby-identifier">arglist</span> = <span class="ruby-value str">'self'</span>
          <span class="ruby-identifier">arglist</span> <span class="ruby-operator">+=</span> <span class="ruby-node">&quot;, #{params}&quot;</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">params</span> <span class="ruby-operator">!=</span> <span class="ruby-value str">''</span>
          <span class="ruby-identifier">method_def</span> = <span class="ruby-value str">&quot;# &lt;i&gt;Extension method&lt;/i&gt; -- Calls the AdWords::Extensions.\#{ext} method\n# with +self+ as the first parameter.\ndef \#{ext}(\#{params})\nreturn AdWords::Extensions.\#{ext}(\#{arglist})\nend\n\n&quot;</span>
          <span class="ruby-identifier">class_def</span> <span class="ruby-operator">+=</span> <span class="ruby-identifier">method_def</span>
        <span class="ruby-keyword kw">end</span>
      <span class="ruby-keyword kw">end</span>

      <span class="ruby-identifier">class_def</span> <span class="ruby-operator">+=</span> <span class="ruby-value str">&quot;end\nend\nend\nend\n&quot;</span>
      <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">class_def</span>
    <span class="ruby-keyword kw">end</span></pre>
                    </div>
                </div>
                
            </div>
            
</div>
    </div>
  </body>
</html>    